package com.kuaimacode.kframework.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.api.vo.request.BindBankCardReq;
import com.kuaimacode.kframework.api.vo.response.BankCardResp;
import com.kuaimacode.kframework.mybatis.mapper.user.UserBankCardAccountMapper;
import com.kuaimacode.kframework.mybatis.models.user.UserBankCardAccount;
import com.kuaimacode.kframework.util.BankCardBinUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author 12786 on 2019/3/30.
 * @version 1.0
 */
@Service
public class UserBankCardAccountService extends ServiceImpl<UserBankCardAccountMapper,UserBankCardAccount> {

    public PageInfo<UserBankCardAccount> getBankCardList(Long userId, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<UserBankCardAccount> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("status","1");
        return new PageInfo<UserBankCardAccount>(baseMapper.selectList(wrapper));
    }


    @Transactional
    public boolean saveBankData(BindBankCardReq cardReq,Long userId){
        UserBankCardAccount bankCardAccount = new UserBankCardAccount();
//        bankCardAccount.setBankCardDesc();
        bankCardAccount.setBankCardNumber(cardReq.getBankCardNumber());
        bankCardAccount.setBankCardDesc(cardReq.getBankCardName());
        bankCardAccount.setBankCardType(cardReq.getBankCardType());
        bankCardAccount.setCardUserName(cardReq.getCardUserName());
        bankCardAccount.setBankId(Long.parseLong(cardReq.getBankCardId()));
        bankCardAccount.setStatus("1");
        bankCardAccount.setCreateDt(new Date());
        bankCardAccount.setUpdateDt(new Date());
        bankCardAccount.setUserId(userId);
        return baseMapper.insert(bankCardAccount)>0;
    }


    public List<BankCardResp> getBankListByUserId(Long userId){
        QueryWrapper<UserBankCardAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("status","1");
        List<UserBankCardAccount> bankCardAccounts =  baseMapper.selectList(queryWrapper);
        List<BankCardResp> bankCardResps = new ArrayList<>();
        for (UserBankCardAccount account:bankCardAccounts){
            BankCardResp resp = new BankCardResp();
            resp.setBankCardNumber(account.getBankCardNumber());
            resp.setBankCardType(account.getBankCardType());
            resp.setBankName(account.getBankCardDesc());
            resp.setId(account.getId());
            resp.setBankCardId(account.getBankId());
            bankCardResps.add(resp);
        }
        return bankCardResps;
    }

    /**
     * 验证银行卡号是否用户已添加
     */
    public boolean checkIsExists(Long userId,String cardNo){

        QueryWrapper<UserBankCardAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("bank_card_number",cardNo.trim());
        queryWrapper.eq("status","1");
        return baseMapper.selectCount(queryWrapper)>0?true:false;


    }



}
